package com.zjc.yohoshop.mvp.persenter;

import com.zjc.lib_core.mvp.presenter.BasePresenter;
import com.zjc.yohoshop.bean.CommunityEntity;
import com.zjc.yohoshop.mvp.contract.CommunityFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CommunityPresenter extends BasePresenter<CommunityFragmentContract.CommunityFragmentIView, CommunityFragmentContract.CommunityFragmentIModel> {

    private int page = 1;

    @Inject
    public CommunityPresenter(CommunityFragmentContract.CommunityFragmentIView communityFragmentIView, CommunityFragmentContract.CommunityFragmentIModel communityFragmentIModel) {
        super(communityFragmentIView, communityFragmentIModel);
    }

    public void request(){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("page", "" + page);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.community(jsonObject.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommunityEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(CommunityEntity entity) {
                        v.hideDialog();
                        v.success(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

    public void refresh() {
        page = 1;
        request();

    }

    public void loadMore() {
        page++;
        request();
    }

}
